
Editor.Geometry = {
	intersectShapes: function(b1x,b1y,b1w,b1h,b2x,b2y,b2w,b2h) {
		return !( (b1y > b2y + b2h ) || (b1y + b1h < b2y) || (b1x > b2x + b2w) || (b1x + b1w < b2x));
	},

	// begin from: http://bloggingmath.wordpress.com/2009/05/29/line-segment-intersection/
	Vector: function(x, y) {
		this.x = x;
		this.y = y;
		this.scalarMult = function(scalar) {
			return new Editor.Geometry.Vector(this.x * scalar, this.y * scalar);
		}
		this.dot = function(v2) {
			return this.x * v2.x + this.y * v2.y;
		};
		this.perp = function() {
			return new Editor.Geometry.Vector( - 1 * this.y, this.x);
		};
		this.subtract = function(v2) {
			return this.add(v2.scalarMult( - 1));
		};
		this.add = function(v2) {
			return new Editor.Geometry.Vector(this.x + v2.x, this.y + v2.y);
		}
	},

	Segment: function(p1, p2) {
		this.p1 = p1;
		this.p2 = p2;
	},
	
	cross: function(v1, v2) {
		return v1.x * v2.y - v2.x * v1.y;
	},

	epsilon: 10e-6,
	DONT_INTERSECT: 0,
	PARALLEL_DONT_INTERSECT: 1,
	INTERSECT: 3,

	intersect: function(seg1, seg2, intersectionPoint) {
		var p = seg1.p1;
		var r = seg1.p2.subtract(seg1.p1);
		var q = seg2.p1;
		var s = seg2.p2.subtract(seg2.p1);
		var rCrossS = Editor.Geometry.cross(r, s);
		if (rCrossS <= Editor.Geometry.epsilon && rCrossS >= -1 * Editor.Geometry.epsilon) {
			return Editor.Geometry.PARALLEL_DONT_INTERSECT;
		}
		var t = Editor.Geometry.cross(q.subtract(p), s) / rCrossS;
		var u = Editor.Geometry.cross(q.subtract(p), r) / rCrossS;
		if (0 <= u && u <= 1 && 0 <= t && t <= 1) {
			var intPoint = p.add(r.scalarMult(t));
			intersectionPoint.x = intPoint.x;
			intersectionPoint.y = intPoint.y;
			return Editor.Geometry.INTERSECT;
		} else {
			return Editor.Geometry.DONT_INTERSECT;
		}
	},

	intersectionShapeAndSegment: function(shape, x1, y1, x2, y2) {
		var seg = new Editor.Geometry.Segment(new Editor.Geometry.Vector(x1, y1), new Editor.Geometry.Vector(x2, y2));

		var shape_x = shape.getX();
		var shape_y = shape.getY();
		var shape_width = shape.getWidth();
		var shape_height = shape.getHeight();

		var A = new Editor.Geometry.Vector(shape_x, shape_y);
		var B = new Editor.Geometry.Vector(shape_x + shape_width, shape_y);
		var C = new Editor.Geometry.Vector(shape_x + shape_width, shape_y + shape_height);
		var D = new Editor.Geometry.Vector(shape_x, shape_y + shape_height);

		var seg_top = new Editor.Geometry.Segment(A, B);
		var seg_right = new Editor.Geometry.Segment(B, C);
		var seg_bottom = new Editor.Geometry.Segment(C, D);
		var seg_left = new Editor.Geometry.Segment(D, A);

		var intersectionPoint = new Editor.Geometry.Vector(0, 0);
		if (Editor.Geometry.intersect(seg, seg_top, intersectionPoint) == Editor.Geometry.INTERSECT) {
			delete A, B, C, D, seg, seg_top, seg_right, seg_bottom, seg_left;
			return intersectionPoint;
		}
		if (Editor.Geometry.intersect(seg, seg_right, intersectionPoint) == Editor.Geometry.INTERSECT) {
			delete A, B, C, D, seg, seg_top, seg_right, seg_bottom, seg_left;
			return intersectionPoint;
		}
		if (Editor.Geometry.intersect(seg, seg_bottom, intersectionPoint) == Editor.Geometry.INTERSECT) {
			delete A, B, C, D, seg, seg_top, seg_right, seg_bottom, seg_left;
			return intersectionPoint;
		}
		if (Editor.Geometry.intersect(seg, seg_left, intersectionPoint) == Editor.Geometry.INTERSECT) {
			delete A, B, C, D, seg, seg_top, seg_right, seg_bottom, seg_left;
			return intersectionPoint;
		}
		delete A, B, C, D, seg, seg_top, seg_right, seg_bottom, seg_left;
	},
	// end from: http://bloggingmath.wordpress.com/2009/05/29/line-segment-intersection/


}
